package com.wmx.hb.controller;

import com.wmx.hb.enums.ResultCode;
import com.wmx.hb.mapper.DatabaseInitMapper;
import com.wmx.hb.pojo.ResultData;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 数据库初始化控制器
 *
 * @author wangMaoXiong
 * @version 1.0
 * @date 2023/4/8 9:14
 */
@RestController
public class DatabaseInitController {

    private static final String TABLE_NAME_PERSON = "person".toUpperCase();

    @Resource
    private DatabaseInitMapper databaseInitMapper;

    /**
     * 初始化人员信息表: http://127.0.0.1:8080/db/init/person
     *
     * @return
     */
    @GetMapping("/db/init/person")
    @Transactional(rollbackFor = Exception.class)
    public ResultData<Object> initForPerson() {
        int existTable = databaseInitMapper.existTable(DatabaseInitController.TABLE_NAME_PERSON);
        System.out.println("existTable=" + existTable);
        databaseInitMapper.dropTable(DatabaseInitController.TABLE_NAME_PERSON);
        databaseInitMapper.createTableForPerson(DatabaseInitController.TABLE_NAME_PERSON);
        int insertPersons = databaseInitMapper.insertPersons(DatabaseInitController.TABLE_NAME_PERSON);
        // insertPersons=5
        System.out.println("insertPersons=" + insertPersons);
        return new ResultData<>(ResultCode.SUCCESS, insertPersons);
    }

}
